package org.ljf.web;

import org.ljf.dao.Seckill;
import org.ljf.dto.Exposer;
import org.ljf.dto.SecKillEcution;
import org.ljf.dto.SecKillResult;
import org.ljf.exception.RepeatKillException;
import org.ljf.exception.SecKillCloseException;
import org.ljf.exception.SecKillException;
import org.ljf.service.SeckillService;
import org.ljf.stateEnum.SecKillStateEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @创建人 lIUJF
 * @创建时间 2018/12/21
 * @描述:
 */

@Controller //注解把当前类放入spring容器
@RequestMapping("/seckill") //url:模块/资源/id/细分
public class SecKillController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private SeckillService seckillService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)//限定为Get请求
	public String list(Model model){
		//获取列表页
		List<Seckill> allSecKiller = seckillService.getAllSecKiller();
		model.addAttribute("list", allSecKiller);
		//list.jsp +model= ModelAndView
		return "list";//WEB-INF/jsp/"list".jsp
	}

	@RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET)
	public String detail(@PathVariable("seckillId") Long secKillId, Model model){
		if (StringUtils.isEmpty(secKillId)) {
			return "redirect:/seckill/list";
		}
		Seckill seckill = seckillService.getById(secKillId);
		if (seckill == null) {
			return "redirect:/seckill/list";
		} else {
			model.addAttribute("seckill", seckill);
		}
		return "detail";
	}

	@RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public SecKillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId){
		SecKillResult<Exposer> result;
		try {
			Exposer exposer = seckillService.expoertSecKillUrl(seckillId);
			result = new SecKillResult<Exposer>(true, exposer);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			result = new SecKillResult<Exposer>(false, e.getMessage());
		}
		return result;
	}

	@RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public SecKillResult<SecKillEcution> execute(@PathVariable("seckillId") Long seckillId, @PathVariable("md5") String md5, @CookieValue(value = "killPhone", required = false) Long phone){
		if(StringUtils.isEmpty(phone)){
			return new SecKillResult<SecKillEcution>(false,"未注册");
		}
		SecKillResult<SecKillEcution> result;
		try {
			SecKillEcution ecution = seckillService.executeKill(md5, seckillId, phone);
			return new SecKillResult<SecKillEcution>(true,ecution);
		}catch (RepeatKillException e){
			SecKillEcution ecution=new SecKillEcution(seckillId, SecKillStateEnum.REPEAT_KILL);
			return new SecKillResult<SecKillEcution>(true,ecution);
		}catch (SecKillCloseException e){
			SecKillEcution ecution=new SecKillEcution(seckillId, SecKillStateEnum.END);
			return new SecKillResult<SecKillEcution>(true,ecution);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			SecKillEcution ecution=new SecKillEcution(seckillId, SecKillStateEnum.INNER_ERROR);
			return new SecKillResult<SecKillEcution>(true,ecution);
		}
	}

	@RequestMapping(value = "/time/now",method = RequestMethod.GET)
	@ResponseBody
	public SecKillResult<Long> time(){
		Date now=new Date();
		return new SecKillResult(true,now.getTime());
	}

}
